package com.jxy.test;

import com.jxy.tool.ObjectInputStreamWithLoader;
import sun.misc.VM;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;

public class Compare {
    public static ClassLoader loader = getLoader();

    public static void main(String[] args) {
        String file_so1 = "/home/googlewell/tool/zkm8/ZKM_SO1.ser";
        String file_so2 = "/home/googlewell/tool/zkm8/ZKM_SO.ser";
        compare(file_so1, file_so2);
    }

    public static void compare(String str1, String str2) {
        Object object1 = readObject(str1);
        Object object2 = readObject(str2);
        System.out.println(object1.hashCode() + ":" + object2.hashCode());
        if (object1.equals(object2)) {
            System.out.println("一致");
        } else {
            System.out.println("不一致");
        }
    }

    public static Object readObject(String filePath) {
        Object object = null;
        try {
            ObjectInputStreamWithLoader oisl = new ObjectInputStreamWithLoader(new FileInputStream(filePath), loader);
            object = oisl.readObject();
            oisl.close();
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
        return object;
    }

    public static ClassLoader getLoader() {
        try {
            String jar = "/home/googlewell/tool/zkm8/ZKM.jar";
            URL[] urls = new URL[]{new File(jar).toURI().toURL()};
            return new URLClassLoader(urls);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        return VM.latestUserDefinedLoader();
    }
}
